<?php

class M_applicants Extends MY_Model
{

	public function get_applicant($e_id = '')
	{
		if($e_id != '') 
		{
		$sql = "SELECT p.firstname,
						p.middlename,
						p.lastname,
						p.name_ext,
						p.previous_school,
						l.level_desc as level_code,
						af.or_no,
						af.payment_date,
						e.e_id,
						e.e_profile_id,
						e.e_grade_level,
						e.schedule_for_testing,
						e.guidance_approved,
						e.guidance_status,
						e.guidance_school_records,
						e.guidance_recommendation,
						e.guidance_recommend_level_id,
						e.vice_principal_interview,
						e.hear_from,
						e.af_paid,
						e.charge_to,
						e.moved_to,
						e.a_f_description,
						e.a_f_price,
						e.applying_level_id
						FROM enrollments e
						LEFT JOIN profiles p ON e.e_profile_id = p.profile_id
						LEFT JOIN levels l ON e.e_grade_level = l.level_id
						LEFT JOIN assessment_fees af ON e.e_id = af.enrollment_id
						WHERE 
						e.e_id = ?";
					
		$query = $this->db->query($sql,array($e_id));		
												 
		return $query-> num_rows() >= 1 ? $query->row() : FALSE;	
		}else{
			$sql = "SELECT p.firstname,
						p.middlename,
						p.lastname,
						p.name_ext,
						p.previous_school,
						l.level_desc as level_code,
						af.or_no,
						af.payment_date,
						e.e_id,
						e.e_profile_id,
						e.e_grade_level,
						e.schedule_for_testing,
						e.guidance_approved,
						e.guidance_status,
						e.guidance_school_records,
						e.guidance_recommendation,
						e.guidance_recommend_level_id,
						e.vice_principal_interview,
						e.hear_from,
						e.af_paid,
						e.charge_to,
						e.moved_to,
						e.a_f_description,
						e.a_f_price,
						e.applying_level_id,
						e.e_id
					FROM enrollments e
					LEFT JOIN profiles p ON e.e_profile_id = p.profile_id
					LEFT JOIN levels l ON e.e_grade_level = l.level_id
					LEFT JOIN assessment_fees af ON e.e_id = af.enrollment_id
					WHERE e.e_ay_id = ?
					AND e.e_enrollee_type = ?
					AND e.e_registered = ?
					";
					
			$query = $this->db->query($sql,array($this->c_sy->id,
												 'new',
												 0));		
												 
			return $query-> num_rows() >= 1 ? $query->result() : FALSE;
		}
	}

	public function update_applicant_enrollment($e_id,$data)
	{
		$this->db->where('e_id', $e_id)->update('enrollments', $data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	public function insert_applicant_assessment_fee($e_id,$data)
	{
		$this->db->insert('assessment_fees',$data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	public function insert_applicant_payee($data)
	{
		$this->db->insert('payees',$data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}

	public function get_payee($student_profile_id)
	{
		$sql = "SELECT *
				FROM payees
				WHERE student_profile_id = ?
				";
					
		$query = $this->db->query($sql,array($student_profile_id));		
												 
		return $query-> num_rows() >= 1 ? $query->row() : FALSE;
	}

	public function get_af($id)
	{
		$sql = "SELECT *
				FROM assessment_fees
				WHERE enrollment_id = ?
				";
					
		$query = $this->db->query($sql,array($id));		
												 
		return $query-> num_rows() >= 1 ? $query->row() : FALSE;
	}
}